package cn.com.haopy.dataes.mongodb.web;


import cn.com.haopy.dataes.db.entity.service.HStore;
import cn.com.haopy.dataes.db.service.service.HStoreService;
import cn.com.haopy.dataes.mongodb.entity.MRecipel;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/test/")
@Slf4j
public class TestController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private HStoreService hStoreService;

    /* @Autowired
     StudentService studentService;
     @Autowired
     HBaseAdminService hBaseAdminService;


     @GetMapping("template")
     public Object template() {
         Map<String, Object> data = new HashMap<>();
         data.put("name", "徐亚才");
         return data;// studentService.selectList("100", "101");
     }

     @GetMapping("tables")
     public Object tables() throws IOException {
         List<String> tables = hBaseAdminService.getAllTableNames();
         return tables;
     }

     @GetMapping("createSpace")
     public Object createSpace() throws IOException {
         Boolean tables = hBaseAdminService.creatSpace("healthy");
         return tables;
     }
     */
    @GetMapping("getData")
    public Object getData() throws Exception {
        //String nameSpace = "healthy";
        ////String table = "h_recipel";
        // String rowKey = "100000000" ;

        // return hBaseAdminService.getDataRowKey(nameSpace,table,rowKey) ;
        HStore store = hStoreService.getById("100001");
        return store;

    }

    @GetMapping("pageData")
    public Object pageData(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false) String keyWord) throws Exception {
        //Query query = new Query();
        //查询记录数 query必须填写可以不构建条件，但不能类似mp直接写null
        //long count = mongoTemplate.count(query, MHRecipel.class);
        //System.out.println("count:"+count);
        Criteria criteria1 = Criteria.where("customer.name").is("徐晓婷");
        //MatchOperation match1 = Aggregation.match(criteria1);
        LookupOperation operation1 = Aggregation.lookup("h_store",
                "storeCode",
                "code",
                "mStore");
        LookupOperation operation2 = Aggregation.lookup(
                "h_customer",
                "customerCode",
                "code",
                "mCustomer");
        LookupOperation operation3 = Aggregation.lookup(
                "h_employee",
                "employeeCode",
                "code",
                "mEmployee");
        CountOperation.CountOperationBuilder countOperationBuilder = Aggregation.count();

        class CountResult {
            int count = 0;
        }

        System.out.println(LocalDateTime.now());

        Query query = new Query();
        //查询记录数 query必须填写可以不构建条件，但不能类似mp直接写null
        long count = mongoTemplate.count(query, MRecipel.class);

        System.out.println(LocalDateTime.now());
        System.out.println("count:" + count);

       /* List<AggregationOperation> aggregationOperationList = Arrays.asList(operation1, operation2, operation3);
        //aggregationOperationList.add(TypedAggregation.match(criteria1));
        aggregationOperationList.add(Aggregation.skip((pageNum - 1) *
                pageSize));
        aggregationOperationList.add(Aggregation.limit(pageSize));
        aggregationOperationList.add(Aggregation.sort(Sort.by(Sort.Order.desc("create_date"))));

        TypedAggregation<MRecipel> tagg = Aggregation.newAggregation(MRecipel.class, aggregationOperationList);*/

        Aggregation aggregation =
                Aggregation.newAggregation(operation1, operation2, operation3, Aggregation.skip((pageNum - 1) *
                        pageSize), Aggregation.limit(pageSize), Aggregation.sort(Sort.by(Sort.Order.desc("create_date"))));
        //aggregation.withOptions(Aggregation.match(criteria1));

        //分页  skip为跳过这几条数据意味着开始位置，limit为每页的数据量
        AggregationResults<MRecipel> users = mongoTemplate.aggregate(aggregation, "h_recipel", MRecipel.class);
        System.out.println(users.getMappedResults());
        System.out.println(LocalDateTime.now());
        return users.getMappedResults();

        //List<MRecipel> users = mongoTemplate.find(query.skip((pageNum - 1) *
        //        pageSize).limit(pageSize), MRecipel.class);
        //System.out.println(LocalDateTime.now());
        //return users ;

    }


}
